#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define len 15 //数组元素个数

void _print(int*, int);
void _sort(int*, int);

int main()
{
    int arr[len] = {0};

    srand(time(NULL)); //设置随机种子
    //printf("%ld\n",rand());
    for(int i = 0; i < len; i++)
    {
        arr[i] = rand() % 1000 + 1; //产生100个随机数
    }
    _print(arr, len);
    printf("\n__________________________________________________________\n");
    _sort(arr, len);
    _print(arr, len);
    printf("\n__________________________________________________________\n");

    int FindValue = 0, count = 0;
    scanf("%d", &FindValue);
    int left = 0, right = len - 1, mid = 0;
    while(left <= right)
    {
        count++;
        mid = (left + right) / 2;
        if(FindValue == arr[mid])
        {
            printf("这个数据的下标为：%d 共查询%d次\n", mid, count);
            //break;
        }
        if(FindValue > arr[mid])
        {
            right = mid - 1;
        }
        else
        {
            left = mid + 1;
        }         
    }
    return 0;
}

void _print(int* array, int n)
{
    int i = 0;
    for(i = 0; i < n; i++)
    {
        printf("%d\t", array[i]);
    }
}
void _sort(int* array, int n)
{
    int i = 0, j = 0, max = 0;
    for(i = 0; i < n - 1; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(array[max] < array[j])
                max = j;
        }
        if(max != i)
        {
            array[max] = array[max] + array[i];
            array[i] = array[max] - array[i];
            array[max] = array[max] - array[i];
        }
    }
}


